package com.htl.test;

import com.htl.entity.Student;
import com.htl.mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class TwoLevelCache {
    public static void main(String[] args) {
        //加载MyBatis配置文件
        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");

        //构建sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(inputStream);

        //获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //通过sqlSession获取代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        Student student = mapper.selectById(1);
        System.out.println(student);

        sqlSession.close();

        //获取新的sqlSession
        sqlSession = factory.openSession();
        mapper = sqlSession.getMapper(StudentMapper.class);
        Student student1 = mapper.selectById(1);
        System.out.println(student1);
    }

}

